// DlgSetActiveLayer.cpp : implementation file
//

#include "stdafx.h"
#include "ModelLayer.h"
#include "DlgSetActiveLayer.h"
#include "afxdialogex.h"
#include "ModelLayerView.h"

// CDlgSetActiveLayer dialog

IMPLEMENT_DYNAMIC(CDlgSetActiveLayer, CDialogEx)

CDlgSetActiveLayer::CDlgSetActiveLayer(CModelLayerView *pView, CWnd* pParent /*=NULL*/)
	: CDialogEx(CDlgSetActiveLayer::IDD, pParent)
{
	m_pParentView = pView;
	m_pActveLayer = NULL;
}

CDlgSetActiveLayer::~CDlgSetActiveLayer()
{
}

void CDlgSetActiveLayer::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_LAYER, m_cbLayers);
}


BEGIN_MESSAGE_MAP(CDlgSetActiveLayer, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_LAYER, &CDlgSetActiveLayer::OnSelchangeComboLayer)
	ON_BN_CLICKED(IDOK, &CDlgSetActiveLayer::OnBnClickedOk)
END_MESSAGE_MAP()


// CDlgSetActiveLayer message handlers


void CDlgSetActiveLayer::OnSelchangeComboLayer()
{
}


BOOL CDlgSetActiveLayer::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	HModelLayerModel *pHmodel = (HModelLayerModel *)m_pParentView->GetHoopsView()->GetModel();
	CSystem *pSys = pHmodel->GetBigDrawing();

	CLayer* pActiveLayer = pSys->GetActiveLayer();
	int nActiveLyerIndex = 0;

	list<CLayer *> layerLst = pSys->GetLayerList();
	list<CLayer *>::iterator it;
	int index=0;
	for (it=layerLst.begin(); it!=layerLst.end(); it++,index++)
	{
		CLayer *pTmpLayer = *it;
		m_mapLayer.insert(make_pair(index, pTmpLayer));
		
		CString szLayerName = pTmpLayer->GetName();
		m_cbLayers.AddString(szLayerName);

		if (pTmpLayer == pActiveLayer)
			nActiveLyerIndex = index;
	}

	m_cbLayers.SetCurSel(nActiveLyerIndex);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


void CDlgSetActiveLayer::OnBnClickedOk()
{
	int nSelIndex = m_cbLayers.GetCurSel();
	CLayer *pSelLayer = m_mapLayer[nSelIndex];

	HModelLayerModel *pHmodel = (HModelLayerModel *)m_pParentView->GetHoopsView()->GetModel();
	CSystem *pSys = pHmodel->GetBigDrawing();
	CLayer* pOldActiveLayer = pSys->GetActiveLayer();
	if (pSys->ActiveLayer(pSelLayer))
	{
		pHmodel->SetLayerVisibility();

// 		HWND hWnd;
// 		CModelObjOption::GetIModelObjEventHandler()->GetHwnd(hWnd);
// 		::PostMessage(hWnd, WM_LAYER_SWITCH, WPARAM(pOldActiveLayer->GetId()), LPARAM(pSelLayer->GetId()));
	}

	m_pParentView->Invalidate(FALSE);

	CDialogEx::OnOK();
}
